package com.fxd.dao.impl;

import com.fxd.dao.IUserDao;
import com.fxd.doman.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

/**
 * @auther fuxudong
 * @date 2020/12/18 - 17:20
 */
public class userdaoimpl implements IUserDao {

    private SqlSessionFactory factory;

    public userdaoimpl(SqlSessionFactory factory) {
        this.factory = factory;
    }

    public List<User> findAll() {
        //1,根据factory获取SQLsession对象
        SqlSession session = factory.openSession();
        //调用SQLsession中的方法，实现查询列表
        List<User> users = session.selectList("com.fxd.dao.IUserDao.findAll");
        session.close();
        return users;

    }

    public void saveUser(User user) {
        SqlSession session = factory.openSession();
        session.insert("com.fxd.dao.IUserDao.saveUser", user);
        session.commit();
        session.close();
    }

    public void updateUser(User user) {
        SqlSession session = factory.openSession();
        session.update("com.fxd.dao.IUserDao.updateUser", user);
        session.commit();
        session.close();

    }

    public void deleteUser(Integer id) {
        SqlSession session = factory.openSession();
        session.delete("com.fxd.dao.IUserDao.deleteUser", id);
        session.commit();
        session.close();


    }

    public User findById(Integer userId) {
        //1,根据factory获取SQLsession对象
        SqlSession session = factory.openSession();
        //调用SQLsession中的方法，实现查询列表
        User user = session.selectOne("com.fxd.dao.IUserDao.findById", userId);
        session.close();
        return user;
    }

    public List<User> findByName(String username) {
        //1,根据factory获取SQLsession对象
        SqlSession session = factory.openSession();
        //调用SQLsession中的方法，实现查询列表
        List<User> users = session.selectList("com.fxd.dao.IUserDao.findByName",username);
        session.close();
        return users;
    }

    public int findtotol() {
        //1,根据factory获取SQLsession对象
        SqlSession session = factory.openSession();
        //调用SQLsession中的方法，实现查询列表
        Integer o = session.selectOne("com.fxd.dao.IUserDao.findtotol");
        session.close();
        return o;
    }
}
